package com.wanli.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanli.domain.entity.Role;
import com.wanli.domain.entity.UserRole;
import com.wanli.mapper.RoleMapper;
import com.wanli.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanli.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Byterain
 * @since 2023-12-06
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private UserRoleService userRoleService;

    @Override
    public List<Role> listRolesByUserId(Long userId) {
        //从sys_user_role关联表中查询
        List<UserRole> user_role_list = userRoleService.list(new QueryWrapper<UserRole>().eq("user_id", userId));

        if(user_role_list.size()>0){
            //取出当前用户所具备的 角色id
            List<Long> roleIds = user_role_list.stream().map(ur -> ur.getRoleId()).collect(Collectors.toList());
            //调用RoleService继承IService, listByIds()  根据所有角色id查询出所有的角色对象
            List<Role> roles = this.listByIds(roleIds);
            return roles;
        }else{
            return null;
        }
    }
}
